﻿using ASPNETVN.PORTAL.Components.UI;
using System;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;
using CUSTOMS.Modules.QLHS.Components.QLBB;
using CUSTOMS.Components;
using ASPNETVN.PORTAL.Components.Security;
using ASPNETVN.PORTAL.Components.Helpers;
using System.Collections.Generic;
using CUSTOMS.Components.SLXNK;

namespace CUSTOMS.Modules.QLHS.Controls
{
    public partial class AddEditBienBan : AddEditItemControl
    {
        protected override void OnInit(EventArgs e)
        {
            this.ButtonUpdate = btnUpdate;
            this.ButtonDelete = btnDelete;
            this.ButtonCancel = btnCancel;

            this.Update += new EventHandler(AddEditBienBan_Update);
            this.Delete += new EventHandler(AddEditBienBan_Delete);
            base.OnInit(e);
        }

        void AddEditBienBan_Delete(object sender, EventArgs e)
        {
            if ((string.IsNullOrEmpty(Request.QueryString["SoToKhai"]) == false) && (string.IsNullOrEmpty(Request.QueryString["MaHaiQuan"]) == false) && (string.IsNullOrEmpty(Request.QueryString["MaLoaiHinh"]) == false) && (string.IsNullOrEmpty(Request.QueryString["NamDangKy"]) == false))
            {
                int sotokhai = Convert.ToInt32(Request.QueryString["SoToKhai"]);
                string mahaiquan = Request.QueryString["MaHaiQuan"];
                string maloaihinh = Request.QueryString["MaLoaiHinh"];
                int namdangky = Convert.ToInt32(Request.QueryString["NamDangKy"]);
                BienBan.DeleteBienBan(sotokhai, mahaiquan, maloaihinh, namdangky);
                string url = HttpUrlBuilder.BuildModulePageUrl(this.ModuleID, 0, ModulePages.TheoDoiBienBan);
                Response.Redirect(url);
            }
        }

        void AddEditBienBan_Update(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                lblMessage.Text = string.Empty;
                // Kiểm tra mã doanh nghiệp.
                string tendoanhnghiep = txtTenDoanhNghiep.Text;

                IList<HangHoa> hanghoa = (IList<HangHoa>)Session["HangHoa"];
                if (hanghoa == null)
                {
                    lblMessage.Text += "Chưa nhập thông tin hàng hóa.<div style='padding:4px'></div>";
                }

                if (lblMessage.Text != string.Empty)
                {
                    return;
                }

                // Lấy thông tin cấu hình của Module.
                Hashtable settings = this.Settings;

                BienBan bienban = new BienBan();

                if ((string.IsNullOrEmpty(Request.QueryString["SoToKhai"]) == false) && (string.IsNullOrEmpty(Request.QueryString["MaHaiQuan"]) == false) && (string.IsNullOrEmpty(Request.QueryString["MaLoaiHinh"]) == false) && (string.IsNullOrEmpty(Request.QueryString["NamDangKy"]) == false))
                { 
                    int sotokhai = Convert.ToInt32(Request.QueryString["SoToKhai"]);
                    string mahaiquan = Request.QueryString["MaHaiQuan"];
                    string maloaihinh = Request.QueryString["MaLoaiHinh"];
                    int namdangky =  Convert.ToInt32(Request.QueryString["NamDangKy"]);
                    bienban = BienBan.Load(sotokhai, mahaiquan, maloaihinh, namdangky);
                }

                if (bienban == null)
                {
                    bienban = new BienBan();
                }

                bienban.SoToKhai = Convert.ToInt32(Request.QueryString["sotokhai"]);
                bienban.MaHaiQuan = Request.QueryString["mahaiquan"];
                bienban.MaLoaiHinh = Request.QueryString["maloaihinh"];
                bienban.NamDangKy = Convert.ToInt32(Request.QueryString["namdangky"]);
                bienban.MaDoanhNghiep = txtMaDoanhNghiep.Text;
                bienban.PhanLuong = 0;
                bienban.SoTo = 0;
                bienban.TruongHop = ddlTruongHop.SelectedValue;
                bienban.MaHaiQuanChuyenDen = ddlDonViHaiQuanChuyenDen.SelectedValue;
                bienban.NoiGiao = settings["MaHaiQuan"].ToString();
                bienban.NguoiGiao = PortalSecurity.CurrentUser.Identity.Email;
                bienban.NoiNhan = txtMaDoanhNghiep.Text;
                bienban.NguoiNhan = txtNguoiDaiDien.Text;
                bienban.CanBoTheoDoi = PortalSecurity.CurrentUser.Identity.Email;
                bienban.TrangThai = "SEND";
                bienban.GhiChu = txtGhiChu.Text;
                bienban.SoBienBan = txtSoBienBan.Text.Trim();

                int gio = Convert.ToInt32(ddlGio.SelectedValue);
                int phut = Convert.ToInt32(ddlPhut.SelectedValue);
                DateTime ngay = Convert.ToDateTime(txtNgay.Text);
                bienban.NgayBanGiao = new DateTime(ngay.Year, ngay.Month, ngay.Day, gio, phut, 0);
                bienban.SoVanTaiDon = txtSoVanTaiDon.Text;
                if (bienban.SoVanTaiDon == string.Empty)
                {
                    bienban.SoVanTaiDon = bienban.SoToKhai + "/" + bienban.MaLoaiHinh + "/" + bienban.MaHaiQuan + "/" + bienban.NamDangKy;
                }

                bienban.SoBanChinhToKhai = Convert.ToInt32(txtSoBanChinhToKhai.Text);
                bienban.SoBanChinhDonChuyenCuaKhau = Convert.ToInt32(txtSoBanChinhDonChuyenCuaKhau.Text);
                bienban.SoBanSaoVanTaiDon = Convert.ToInt32(txtSoBanSaoVanTaiDon.Text);
                bienban.SoBanChinhBanKeChiTiet = Convert.ToInt32(txtSoBanChinhBanKeChiTiet.Text);
                bienban.MoTaHangHoa = txtMoTaHangHoa.Text;

                bienban.TinhTrangHangHoa = txtTinhTrangHangHoa.Text;
                bienban.BienBanVeTinhTrangHangHoa = txtBienBanVeTinhTrangHangHoa.Text;
                bienban.TuyenDuongVanChuyen = txtTuyenDuongVanChuyen.Text;
                bienban.ChieuDai = Convert.ToDecimal(txtChieuDai.Text);
                bienban.ThoiGianVanChuyen = Convert.ToDecimal(txtThoiGianVanChuyen.Text);

                gio = Convert.ToInt32(ddlGioXuatPhat.SelectedValue);
                phut = Convert.ToInt32(ddlPhutXuatPhat.SelectedValue);
                ngay = Convert.ToDateTime(txtNgayXuatPhat.Text);
                bienban.GioXuatPhat = new DateTime(ngay.Year, ngay.Month, ngay.Day, gio, phut, 0);

                bienban.CacVanDeLienQuan = txtCacVanDeLienQuan.Text;
                bienban.NguoiNhapDuLieu = PortalSecurity.CurrentUser.Identity.Email;
                bienban.NgayNhapDuLieu = DateTime.Now;

                foreach (HangHoa item in hanghoa)
                {
                    item.SoToKhai = bienban.SoToKhai;
                    item.MaHaiQuan = bienban.MaHaiQuan;
                    item.MaLoaiHinh = bienban.MaLoaiHinh;
                    item.NamDangKy = bienban.NamDangKy;
                }

                bienban.Create(hanghoa);
                Response.Redirect(HttpUrlBuilder.BuildModulePageUrl(this.ModuleID, this.ItemID, ModulePages.InBienBan, "sotokhai={0}&mahaiquan={1}&maloaihinh={2}&namdangky={3}", bienban.SoToKhai, bienban.MaHaiQuan, bienban.MaLoaiHinh, bienban.NamDangKy));
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack == false)
            {
                Hashtable settings = this.Settings;
                if (Session["HangHoa"] != null)
                {
                    Session.Remove("HangHoa");
                }

                txtMaDonViHaiQuanChuyenDen.Attributes.Add("onchange", string.Format("handler({0}, {1})", txtMaDonViHaiQuanChuyenDen.ClientID, ddlDonViHaiQuanChuyenDen.ClientID));
                ddlDonViHaiQuanChuyenDen.Attributes.Add("onchange", string.Format("handler({1}, {0})", txtMaDonViHaiQuanChuyenDen.ClientID, ddlDonViHaiQuanChuyenDen.ClientID));
                txtMaDonViHaiQuanChuyenDen.Text = ddlDonViHaiQuanChuyenDen.SelectedValue = settings["MaHaiQuanChuyenDen"].ToString();

                for (int d = 0; d < 24; d++)
                {
                    ddlGio.Items.Add(new ListItem(d.ToString(), d.ToString()));
                    ddlGioXuatPhat.Items.Add(new ListItem(d.ToString(), d.ToString()));
                }

                for (int m = 0; m < 60; m++)
                {
                    ddlPhut.Items.Add(new ListItem(m.ToString(), m.ToString()));
                    ddlPhutXuatPhat.Items.Add(new ListItem(m.ToString(), m.ToString()));
                }

                DateTime now = DateTime.Now;
                txtNgay.Text = txtNgayXuatPhat.Text = now.ToShortDateString();

                ddlGio.SelectedValue = now.Hour.ToString();
                ddlPhut.SelectedValue = now.Minute.ToString();

                using (IDataReader reader = DanhMuc.GetDonViHaiQuan(false))
                {
                    ddlDonViHaiQuanChuyenDen.DataSource = reader;
                    ddlDonViHaiQuanChuyenDen.DataBind();
                    reader.Close();
                }

                if ((string.IsNullOrEmpty(Request.QueryString["SoToKhai"]) == false) && (string.IsNullOrEmpty(Request.QueryString["MaHaiQuan"]) == false) && (string.IsNullOrEmpty(Request.QueryString["MaLoaiHinh"]) == false) && (string.IsNullOrEmpty(Request.QueryString["NamDangKy"]) == false))
                {
                    int sotokhai = Convert.ToInt32(Request.QueryString["SoToKhai"]);
                    string mahaiquan = Request.QueryString["MaHaiQuan"];
                    string maloaihinh = Request.QueryString["MaLoaiHinh"];
                    int namdangky = Convert.ToInt32(Request.QueryString["NamDangKy"]);

                    using (IDataReader reader = ToKhaiMauDich.TimKiem(sotokhai, maloaihinh, mahaiquan, namdangky))
                    {
                        if (reader.Read())
                        {
                            string madoanhnghiep = reader["Ma_DV"].ToString().Trim();
                            txtMaDoanhNghiep.Text = madoanhnghiep;
                            txtTenDoanhNghiep.Text = DanhMuc.GetTenDoanhNghiep(madoanhnghiep).Trim();
                        }
                        reader.Close();
                    }

                    txtSoVanTaiDon.Text = sotokhai + "/" + maloaihinh + "/" + mahaiquan + "/" + namdangky;

                    BienBan bienban = BienBan.Load(sotokhai, mahaiquan, maloaihinh, namdangky);
                    if (bienban != null)
                    {
                        //txtSoToKhai.Text = bienban.SoToKhai;
                        //txtMaHaiQuan.Text = bienban.MaHaiQuan;
                        //txtMaLoaiHinh.Text = bienban.MaLoaiHinh;
                        //txtNamDangKy.Text = bienban.NamDangKy;
                        txtMaDoanhNghiep.Text = bienban.MaDoanhNghiep;
                        //txtPhanLuong.Text = bienban.PhanLuong;
                        //txtSoTo.Text = bienban.SoTo;
                        ddlTruongHop.SelectedValue = bienban.TruongHop;
                        txtMaDonViHaiQuanChuyenDen.Text = ddlDonViHaiQuanChuyenDen.SelectedValue = bienban.MaHaiQuanChuyenDen;
                        //txtNoiGiao.Text = bienban.NoiGiao;
                        //txtNguoiGiao.Text = bienban.NguoiGiao;
                        txtMaDoanhNghiep.Text = bienban.MaDoanhNghiep;
                        txtNguoiDaiDien.Text = bienban.NguoiNhan;
                        // Ngay ban giao / Thoi gian lam bien ban.
                        ddlGio.SelectedValue = bienban.NgayBanGiao.Hour.ToString("N0");
                        ddlPhut.SelectedValue = bienban.NgayBanGiao.Minute.ToString("N0");
                        txtNgay.Text = bienban.NgayBanGiao.ToShortDateString();
                        //txtCanBoTheoDoi.Text = bienban.CanBoTheoDoi;
                        //txtTrangThai.Text = bienban.TrangThai;
                        txtGhiChu.Text = bienban.GhiChu;
                        txtSoBienBan.Text = bienban.SoBienBan;
                        txtSoVanTaiDon.Text = bienban.SoVanTaiDon;
                        txtSoBanChinhToKhai.Text = bienban.SoBanChinhToKhai.ToString("N0");
                        txtSoBanChinhDonChuyenCuaKhau.Text = bienban.SoBanChinhDonChuyenCuaKhau.ToString("N0");
                        txtSoBanSaoVanTaiDon.Text = bienban.SoBanSaoVanTaiDon.ToString("N0");
                        txtSoBanChinhBanKeChiTiet.Text = bienban.SoBanChinhBanKeChiTiet.ToString("N0");
                        txtMoTaHangHoa.Text = bienban.MoTaHangHoa;
                        txtTinhTrangHangHoa.Text = bienban.TinhTrangHangHoa;
                        txtBienBanVeTinhTrangHangHoa.Text = bienban.BienBanVeTinhTrangHangHoa;
                        txtTuyenDuongVanChuyen.Text = bienban.TuyenDuongVanChuyen;
                        txtChieuDai.Text = bienban.ChieuDai.ToString();
                        txtThoiGianVanChuyen.Text = bienban.ThoiGianVanChuyen.ToString();
                        // Thoi gian xuat phat.
                        txtNgayXuatPhat.Text = bienban.GioXuatPhat.ToShortDateString();
                        ddlGioXuatPhat.SelectedValue = bienban.GioXuatPhat.Hour.ToString("N0");
                        ddlPhutXuatPhat.SelectedValue = bienban.GioXuatPhat.Minute.ToString("N0");
                        txtCacVanDeLienQuan.Text = bienban.CacVanDeLienQuan;
                        //txtNguoiNhapDuLieu.Text = bienban.NguoiNhapDuLieu;
                        //txtNgayNhapDuLieu.Text = bienban.NgayNhapDuLieu;

                        IList<HangHoa> hanghoa = HangHoa.SelectCollectionBy_SoToKhai_AND_MaHaiQuan_AND_MaLoaiHinh_AND_NamDangKy(bienban.SoToKhai, bienban.MaHaiQuan, bienban.MaLoaiHinh, bienban.NamDangKy);
                        gvHangHoa.DataSource = hanghoa;
                        gvHangHoa.DataBind();

                        Session["HangHoa"] = hanghoa;
                    }
                }
            }
        }

        protected void btnThemHang_Click(object sender, EventArgs e)
        {
            IList<HangHoa> hanghoa = (IList<HangHoa>)Session["HangHoa"];
            if (hanghoa == null)
            {
                hanghoa = new List<HangHoa>();

            }
            int max = 0;
            foreach (HangHoa item in hanghoa)
            {
                if (item.STT > max) max = item.STT;
            }

            HangHoa h = new HangHoa();
            h.STT = max + 1;
            h.SoHieuContainer = txtSoHieuContainer.Text;
            h.SoSealContainer = txtSoSealContainer.Text;
            h.SoSealHaiQuan = txtSoSealHaiQuan.Text;
            h.GhiChu = txtGhiChuHangHoa.Text;
            hanghoa.Add(h);
            Session["HangHoa"] = hanghoa;
            gvHangHoa.DataSource = hanghoa;
            gvHangHoa.DataBind();
            txtSoHieuContainer.Text = txtSoSealContainer.Text = txtSoSealHaiQuan.Text = txtGhiChuHangHoa.Text = string.Empty;
        }

        protected void gvHangHoa_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            IList<HangHoa> hanghoa = (IList<HangHoa>)Session["HangHoa"];
            if (hanghoa != null)
            {
                for (int i = 0; i < hanghoa.Count; i++)
                {
                    if (((HangHoa)hanghoa[i]).STT == Convert.ToInt32(gvHangHoa.DataKeys[e.RowIndex].Value))
                    {
                        hanghoa.RemoveAt(i);
                        break;
                    }
                }

                // Thiết lập lại STT.
                int stt = 1;
                foreach (HangHoa item in hanghoa)
                {
                    item.STT = stt;
                    stt++;
                }

                Session["HangHoa"] = hanghoa;
                gvHangHoa.DataSource = hanghoa;
                gvHangHoa.DataBind();
            }
        }
    }
}